<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')"
             :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="任务名称" prop="title">
				<el-input v-model="form.title" placeholder="自动生成" clearable autocomplete="off" readonly></el-input>
			</el-form-item>
			<el-form-item label="任务状态" prop="status">
				<el-select v-model="form.status" placeholder="选择任务状态" filterable>
					<el-option v-for="item in task_status" :key="item.value" :label="item.name" :value="Number(item.value)"/>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '修改任务状态',
				edit: '修改任务状态',
				show: '修改任务状态'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				status: "",
			},
			//验证规则
			rules: {
				apply_cate: [
					{ required: true, message: '请选择任务状态' }
				],
			},
			task_status:[]
		}
	},
	mounted() {
		this.getContract()
	},
	methods: {
		/**获取数据字典*/
		async getContract() {
			var res = await this.$API.common.getDictData.get({
				type: 'task_status'
			});
			this.task_status = res.data.task_status
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var reqData = { id: this.form.id,field:'status',value:this.form.status }
					var res = await this.$API.task.updateStatus.post(reqData);
					if (res.code == 1) {
						this.visible = false;
						this.$emit('success', this.form, this.mode)
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, '提醒', {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
            this.form.status = data.status
            this.form.title = data.title
			this.form.id = data.id
		}
	}
}
</script>

<style></style>
